package com.ruoyi.project.tickets.controller;

import java.util.List;

import com.ruoyi.project.tickets.domain.constant.TicketsConstant;
import com.ruoyi.project.tickets.domain.vo.SerialVo;
import com.ruoyi.project.tickets.service.IShopUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.tickets.domain.Serial;
import com.ruoyi.project.tickets.service.ISerialService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 核销表Controller
 *
 * @author 孙山伟
 * @date 2023-07-01
 */
@RestController
@RequestMapping("/tickets/serial")
public class SerialController extends BaseController
{
    @Autowired
    private ISerialService serialService;
    @Autowired
    private IShopUserService shopUserService;
    /**
     * 查询核销表列表
     */
    @PreAuthorize("@ss.hasPermi('tickets:serial:print')")
    @GetMapping("/print/{ids}")
    public AjaxResult list(@PathVariable Long[] ids)
    {

       serialService.rePrintList(ids);
        return AjaxResult.success();
    }
    /**
     * 查询核销表列表
     */
    @PreAuthorize("@ss.hasPermi('tickets:serial:printList')")
    @GetMapping("/printList")
    public TableDataInfo printList()
    {
        Serial serial=new Serial();
        serial.setIsOff(TicketsConstant.IS_NO_OFF);
        serial.setShopId(shopUserService.getShopIdBySystemUser());
        startPage();
        List<SerialVo> list = serialService.selectSerialList(serial);
        return getDataTable(list);
    }
    /**
     * 查询核销表列表
     */
    @PreAuthorize("@ss.hasPermi('tickets:serial:list')")
    @GetMapping("/list")
    public TableDataInfo list(Serial serial)
    {
        startPage();
        List<SerialVo> list = serialService.selectSerialList(serial);
        return getDataTable(list);
    }

    /**
     * 导出核销表列表
     */
    @PreAuthorize("@ss.hasPermi('tickets:serial:export')")
    @Log(title = "核销表", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Serial serial)
    {
        List<SerialVo> list = serialService.selectSerialList(serial);
        ExcelUtil<SerialVo> util = new ExcelUtil<SerialVo>(SerialVo.class);
        return util.exportExcel(list, "serial");
    }

    /**
     * 获取核销表详细信息
     */
    @PreAuthorize("@ss.hasPermi('tickets:serial:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(serialService.selectSerialById(id));
    }

    @GetMapping(value = "selectBySerNumber/{serialNumber}")
    public AjaxResult selectBySerNumber(@PathVariable("serialNumber") String serialNumber)
    {
        return AjaxResult.success(serialService.selectBySerialNumber(serialNumber));
    }

    /**
     * 新增核销表
     */
    @PreAuthorize("@ss.hasPermi('tickets:serial:add')")
    @Log(title = "核销表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Serial serial)
    {

        return toAjax(serialService.insertSerial(serial));
    }

    /**
     * 根据id核销票券
     * @param id
     * @return
     */
    @GetMapping("/goOff/{id}")
    public AjaxResult goOff(@PathVariable Long id)
    {
        Serial serial=serialService.selectSerialById(id);
        if(TicketsConstant.IS_OFF.equals(serial.getIsOff())){
            return AjaxResult.error("票券已核销！");
        }
        return toAjax(serialService.goOff(id));
    }
    /**
     * 修改核销表
     */
    @PreAuthorize("@ss.hasPermi('tickets:serial:edit')")
    @Log(title = "核销表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Serial serial)
    {
        return toAjax(serialService.updateSerial(serial));
    }

    /**
     * 删除核销表
     */
    @PreAuthorize("@ss.hasPermi('tickets:serial:remove')")
    @Log(title = "核销表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(serialService.deleteSerialByIds(ids));
    }
}
